<template>
    <div class="title">
        <el-form v-model="user" label-width="100px">
            <el-form-item label="用户名">
                <el-input v-model="user.name"></el-input>
            </el-form-item>
            <el-form-item label="性别">
                <el-radio-group v-model="user.sex">
                    <el-radio label="男" value="男" />
                    <el-radio label="女" value="女" />
                </el-radio-group>
            </el-form-item>
<!--            <el-form-item label="住址">-->
<!--                <el-select v-model="user.addr" aria-placeholder="请选择地址">-->
<!--                    <el-option value="潍坊"></el-option>-->
<!--                    <el-option value="济宁"></el-option>-->
<!--                </el-select>-->
<!--            </el-form-item>-->
          <el-form-item label="住址">
            <el-select v-model="user.addr" >
              <el-option v-for="a in addr2" :key="a.key" :value="a.value" :lable="a.value"/>
            </el-select>
          </el-form-item>

            <el-form-item label="爱好">
                <el-checkbox-group v-model="user.hobby">
                    <el-checkbox label="篮球 " name="type" />
                    <el-checkbox label="羽毛球 " name="type" />
                    <el-checkbox label="足球 " name="type" />
                    <el-checkbox label="网球 " name="type" />
                </el-checkbox-group>
            </el-form-item>
        </el-form>

        <el-button type="primary" @click="onSubmit">注册</el-button>
        <el-button type="primary" @click="getMessage">查询</el-button>

        <el-table :data="tableData">
            <el-table-column label="用户名" prop="name"></el-table-column>
            <el-table-column label="性别" prop="sex"></el-table-column>
            <el-table-column label="地址" prop="addr"></el-table-column>
            <el-table-column label="爱好" prop="hobby"></el-table-column>
        </el-table>


    </div>
</template>

<script>
import axios from 'axios'
import {regiestUser,getAllUser} from '@/api'
import qs from 'qs'
export default {
    name: 'First',
    data() {
        return {
            user: {
                name: '',
                sex: '',
                addr: '',
                hobby: [],
            },
            tableData: [],
          addr2: [
            {key: '1',
            value: '潍坊'},
            {key: '2',
            value: '济宁'},
            {key: '3',
            value: '北京'},
            {key: '4',
            value: '南京'},

          ]
        }
    },
    methods: {
        onSubmit() {
            regiestUser(JSON.stringify(this.user)).then(res=>{
                alert(res.data)
                console.log(res);
            }).catch(err=>{
                console.log(err);
            })
            // axios.post('http://localhost:8083/register', JSON.stringify(this.user), {
            //     headers: {
            //         'Content-Type': 'application/json;charset=UTF-8',//设置请求头请求格式为JSON
            //     },
            // }).then(
            //     (res) => {
            //         alert('success')
            //         console.log(res);
            //     }
            // )
        },
        getMessage() {

            getAllUser().then(
                res=>{
                    this.tableData=res.data
                    console.log(res);
                }
            )


            // axios.get('http://localhost:8083/getUser', {
            //     headers: {
            //         'Content-Type': 'application/json;charset=UTF-8',//设置请求头请求格式为JSON
            //     },
            // }).then(
            //     (res) => {
            //         console.log("用户数据", res.data);
            //         this.tableData = res.data;
            //     }
            // )
        }


    }
}

</script>

<style>
.title {
    font-size: large;
    width: 500px;
    margin: 10px auto;
}
</style>
